/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : pop_note_view.js                                 
*  Description  : 쪽지보기
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-07-18  김효순    Created.  
*******************************************************************************/

jQuery(function($){
	function clsDisplay()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsDisplay.prototype = {
		_init: function(){
			var $layer = $("div.pw_area02");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.setInfo({
				"동호회명": ""
				, "날짜": ""
				, "시간": ""
				, "내용": ""
			});
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			this.display();
		}
		, setMemberInfo: function(thisData){
			this.infoData["회원정보"] = thisData;
			this.displayMember();
		}
		, display: function(){
			var $layer = this.$getLayer();
			var titleName = {
				"IN": "보낸사람"
				, "OUT": "받는사람"
			}
			
			var dTitleName = {
				"IN": "발송일"
				, "OUT": "수신일"
			}
			
			$layer.find("dl.view dt:eq(0)").html(titleName[this.infoData["송수신코드"]]);
			$layer.find("dl.view dd.club_sece").html(this.infoData["동호회명"] + " <span class='name' />");
			$layer.find("dl.view dt:eq(1)").html(dTitleName[this.infoData["송수신코드"]]);
			$layer.find("dl.view dd:eq(1)").html(this.infoData["날짜"] + " " + this.infoData["시간"]);
			$layer.find("div.pop_box").html(this.infoData["내용"]);
		}
		, displayMember: function(){
			var $layer = this.$getLayer();
			$layer.find("dl.view dd.club_sece span.name").html(this.infoData["회원정보"]["닉네임"]);
		}
		, getMemberId: function(){
			return this.infoData["회원정보"]["아이디"];
		}
	}
	
	// 버튼
	function clsButton()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsButton.prototype = {
		_init: function(){
			var $layer = $("p.pop_bt");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("img").hide();
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this.$getLayer();
			
			$layer.find("img[alt='삭제']").show();
			
			if(this.infoData["보관여부"] != "Y") $layer.find("img[alt='보관']").show();
			if(this.infoData["송수신코드"] == "IN") $layer.find("img[alt='답장']").show();
		}
	}
	
	function clsComm()
	{
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){

		}
		// 내용
		, getNoteInfo: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "getMessageDetail";
			var optionName = {
				"IN": "messageInSeq"
				, "OUT": "messageOutSeq"
			}
			var options = {
				
			}
			options[optionName[arg["송수신코드"]]] = arg["코드"];
			_gf.ajax({
				"sid": "MG001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": {}
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					var obj = items;
					var cName = "";
					if(_util.nvl(obj["senderType"]) == "C"){
						cName = _util.nvl(obj["club"]["clubName"])
					}
					
					var userid = "";
					var d = "";
					var t = "";
					switch(arg["송수신코드"]){
						case "IN":
							userid = _util.nvl(obj["senderUserId"]);
							d = _util.nvl(obj["sendDate"])+"";
							t = _util.nvl(obj["sendTime"])+"";
							break;
						case "OUT":
							userid = _util.nvl(obj["recUserId"]);
							d = _util.nvl(obj["recDate"])+"";
							t = _util.nvl(obj["recTime"])+"";
							break;
					}
					
					res["결과값"] = {
						"동호회명": cName
						, "회원정보": {
							"아이디": userid
						}
						, "날짜": d.substrEx(0, 4) + "-" + d.substrEx(4, 2) + "-" + d.substrEx(6, 2)
						, "시간": t.substrEx(0, 2) + ":" + t.substrEx(2, 2)
						, "내용": _util.enter2br(_util.nvl(obj["messageBody"]))
						, "송수신코드": arg["송수신코드"]
						, "보관여부": arg["보관여부"]
					}
				}
				
				document.objControl.finishNoteInfo(res);
			});
		}
		// 쪽지보관
		, noteSave: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "saveMessage";
			var optionName = {
				"IN": "messageInSeq"
				, "OUT": "messageOutSeq"
			}
			var options = {
				
			}
			options[optionName[arg["송수신코드"]]] = arg["코드"];
			_gf.ajax({
				"sid": "MG001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				document.objControl.finishNoteSave(res);
			});
		}
		// 쪽지삭제
		, noteDelete: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "deleteMessage";
			var optionName = {
				"IN": "messageInSeq"
				, "OUT": "messageOutSeq"
			}
			var options = {
				
			}
			options[optionName[arg["송수신코드"]]] = arg["코드"];
			if(arg["보관여부"] != "Y") options["inOutFlag"] = arg["송수신코드"];
			_gf.ajax({
				"sid": "MG001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				document.objControl.finishNoteDelete(res);
			});
		}
	}
	
	
	function clsControl()
	{
		this.objComm = null;
		this.objDisplay = null;
		this.objButton = null;
		
		this.openerControl = null;
		
		this.q_seq = null;
		this.q_type = null;
		this.q_ynSaved = null;
		
		this._permitStart = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objDisplay = new clsDisplay();
			this.objButton = new clsButton();
			
			this._beforeStart();
			this._bind();
		}
		, _preload: function(){
			_gf.spinStart();
			
			if(opener){
				if(opener.document.objControl){
					this.openerControl = opener.document.objControl;
				}
			}
			
			if(!this.openerControl){
				self.close();
				return;
			}
			
			this.q_seq = _util.getParam("q_seq");
			this.q_type = _util.getParam("q_type");
			this.q_ynSaved = _util.getParam("q_ynSaved");
			if(!this.q_seq){
				alert("비 정상적 접근입니다.");
				self.close();
				return;
			}
		}
		, _beforeStart: function(){
			var thisClass = this;
			
			this._permitStart = {
				
			}
			
			_util.executeThread(function(){
				for(var key in thisClass._permitStart){
					if(!thisClass._permitStart[key]) return false;
				}
				
				return true;
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _bind: function(){
			var thisClass = this;
			
			$(document).on("click", "img[alt='close']", function(){
				self.close();
			});
			
			this.objButton.$getLayer().on("click", "img[alt='답장'], img[alt='보관'], img[alt='삭제']", function(){
				var alt = $(this).attr("alt");
				
				switch(alt){
					case "답장":
						thisClass.goWritePage(thisClass.objDisplay.getMemberId());
						break;
					case "보관":
						thisClass.noteSave();
						break;
					case "삭제":
						thisClass.noteDelete();
						break;
				}
			});
		}
		, _start: function(){
			// 정보요청
			_gf.spinStart();
			
			this.objComm.getNoteInfo({
				"송수신코드": this.q_type
				, "코드": this.q_seq
				, "보관여부": this.q_ynSaved
			});
		}
		// 정보완료
		, finishNoteInfo: function(ret){
			var thisClass = this;
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			_gf.spinEnd();
			
			this.objDisplay.setInfo(ret["결과값"]);
			this.objButton.setInfo(ret["결과값"]);
			
			_util.setWindowSize();
			
			// 회원정보 요청
			_gf.getMemberInfo({
				"아이디": ret["결과값"]["회원정보"]["아이디"]
				, "콜백": function(res, info){
					thisClass.objDisplay.setMemberInfo(info);
				}
			});
		}
		// 쪽지 보관 완료
		, finishNoteSave: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			_gf.spinEnd();
			
			// 쪽지 리스트
			this.getNoteList();
		}
		// 쪽지 삭제 완료
		, finishNoteDelete: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			opener.location.reload();
			self.close();
		}
		// 보관
		, noteSave: function(){
			_gf.spinStart("saving...");
			this.objComm.noteSave({
				"송수신코드": this.q_type
				, "코드": this.q_seq
			});
		}
		// 삭제
		, noteDelete: function(){
			if(!confirm("삭제하시겠습니까?")) return;
			
			_gf.spinStart("deleting...");
			var options = {
				"코드": this.q_seq
			}
			
			this.objComm.noteDelete({
				"송수신코드": this.q_type
				, "코드": this.q_seq
				, "보관여부": this.q_ynSaved
			});
		}
		// 답장 이동
		, goWritePage: function(member_id){
			var szLink = _ROOT + "/popup/pop_note_send.html?q_id=" + member_id;
			document.location.href = szLink;
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});